#include "at32f403a_407_wk_config.h"
#include "wk_acc.h"
#include "wk_tmr.h"
#include "wk_usart.h"
#include "wk_gpio.h"
#include "wk_system.h"
#include "wk_dma.h"
#include "usbd_core.h"
#include "cdc_msc_class.h"
#include "cdc_msc_desc.h"
#include "usbd_int.h"
#include "ff.h"
#include "malloc.h"
#include "DAPA_config.h"
#include "operate_spim.h"
#include "st7735.h"
#include "board_init.h"
#include "firmwares_burn.h"
#include "app_main.h"
#include "Os_cpu.h"

// #include "uart_isp.h"
#include "string.h"

usbd_core_type usb_core_dev;

// static const uint32_t version  __attribute__((section(".ARM.__at_"  INT_TO_STR(0x8001F00)))) __attribute__((used)) = 0x76543210;



unsigned int TASK_0_STK[2000] __attribute__((aligned(8)));
unsigned int TASK_1_STK[2000] __attribute__((aligned(8)));
unsigned int TASK_2_STK[2000] __attribute__((aligned(8)));



void task_0_main(void)
{
    while(1)
    {
        log("task_0_main\r\n");
        OSTimeDly(1000);
    }
}

void task_1_main(void)
{
    while(1)
    {
        log("task_1_main\r\n");
        OSTimeDly(2000);
    }
}


void task_2_main(void)
{
    while(1)
    {
        log("task_2_main\r\n");
        OSTimeDly(3000);
    }
}



uint8_t  __attribute__((aligned(4))) data_buffer[] = {0x01, 0x01, 0x02, 0x03} ;

int main(void)
{
    wk_system_clock_config();
    wk_periph_clock_config();
    wk_nvic_config();
    wk_acc_init();
    wk_timebase_init();
    
    wk_gpio_config();
    
    wk_usart1_init();
    wk_uart5_init();
    
    wk_tmr3_init();
    wk_tmr6_init();
    // wk_tmr8_init();
    
    // spim_init();
    
    // crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
    // usbd_core_init(&usb_core_dev, USB, &cdc_msc_class_handler, &cdc_msc_desc_handler, 0);
    // usbd_connect(&usb_core_dev);
    
    // ST7735_Init();
    // my_mem_init();
    // Board_Init();
    
    delay_ms(1000);
    delay_ms(1000);
    crc_data_reset();
    
    __IO uint32_t crc_value = 0;
    
    crc_value = crc_block_calculate((uint32_t *)data_buffer, 1);
    
    log("crc_value = %x\r\n",crc_value);
    
    log("Hello World\r\n");
    
    
    
    Task_Create(task_0_main,&TASK_0_STK[32],0);
    Task_Create(task_1_main,&TASK_1_STK[32],1);
    Task_Create(task_2_main,&TASK_2_STK[32],2);
    
    OS_Start();
    
    while(1)
    {
        // ISP_task_func();
        // usb_to_uart();
    }
}



/**

data_len = usb_vcp_get_rxdata(&usb_core_dev, usb_buffer);

    if(data_len > 0 || send_zero_packet == 1)
    {

      // bulk transfer is complete when the endpoint does one of the following
         1 has transferred exactly the amount of data expected
         2 transfers a packet with a payload size less than wMaxPacketSize or transfers a zero-length packet
      //
      if(data_len > 0)
        send_zero_packet = 1;

      if(data_len == 0)
        send_zero_packet = 0;

      timeout = 5000000;
      do
      {
        // send data to host 
        if(usb_vcp_send_data(&usb_core_dev, usb_buffer, data_len) == SUCCESS)
        {
          break;
        }
      }while(timeout --);


*/